package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity;
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas;
import com.hyphenate.easeui.widget.EaseChatExtendMenu;
import com.hyphenate.easeui.widget.EaseChatInputMenu;
import com.hyphenate.easeui.widget.EaseChatMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase;
import com.joplus.housekeeper.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Created by zisheng on 2017/9/21.
 */

public class EaseChatMenuAdapter extends PagerAdapter {
    EaseChatInputMenu easeChatInputMenu;
    private List<EaseEmojiconGroupEntity> emojiconGroupList;
    private List<View> views;
    private EaseChatExtendMenu chatExtendMenu;
    private final LayoutInflater layout;
    int[] tabs = new int[]{R.drawable.chat_pic, R.drawable.chat_rabit, R.drawable.chat_face};
    private EaseEmojiconMenuBase emoji;
    private EaseEmojiconMenu easeEmojiconMenu;

    public EaseChatMenuAdapter(EaseChatInputMenu easeChatInputMenu, List<View> objects) {
        this.easeChatInputMenu = easeChatInputMenu;
        views = objects;
        layout = LayoutInflater.from(easeChatInputMenu.getContext());
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        if (position == 0) {

            chatExtendMenu = (EaseChatExtendMenu) view.findViewById(R.id.extend_menu);
            easeChatInputMenu.registerExtendMenuItem();
            chatExtendMenu.init();
        } else if (position == 1) {
            ViewPager vp = (ViewPager) view.findViewById(R.id.vp);
            vp.setAdapter(new RabitVpAdapter(easeChatInputMenu));
        } else {
            easeEmojiconMenu = (EaseEmojiconMenu) view.findViewById(R.id.EaseEmojiconMenu);
            if (emojiconGroupList == null) {
                emojiconGroupList = new ArrayList<EaseEmojiconGroupEntity>();
                emojiconGroupList.clear();
                emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1, Arrays.asList(EaseDefaultEmojiconDatas.getData())));
            }
            easeEmojiconMenu.init(emojiconGroupList);
            easeChatInputMenu.addEmojiListener();
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public EaseChatExtendMenu getChatExtendMenu() {
        return chatExtendMenu;
    }

    public View getTabView(int position) {
        View view = layout.inflate(R.layout.tab_image, null);
        ImageView iv = (ImageView) view.findViewById(R.id.iv);
        iv.setImageResource(tabs[position]);
        return view;
    }

    public EaseEmojiconMenuBase getEmoji() {
        return easeEmojiconMenu;
    }
}
